package com.zf.shuashua.advert;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;

import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.bean.ConfigAdvertList;
import com.zf.shuashua.bean.advert.AdvertConfigBean;
import com.zf.shuashua.bean.advert.AdvertConfigList;
import com.zf.shuashua.bean.advert.PatchConfigBean;
import com.zf.shuashua.bean.advert.RewardConfigBean;


/**
 * 贴片广告广告管理类
 * 包括穿山甲、优量汇；以及平台之前的互相补量
 */
public class NativeAdvertManager {

    //穿山甲和优量汇相互切换
    private boolean isByte = false;

    private static NativeAdvertManager instance;

    private static final int DELAY_TIME = 4000;

    private boolean isError = false;

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (msg.obj != null && msg.obj instanceof InteractionAdListener) {
                    InteractionAdListener listener = (InteractionAdListener) msg.obj;
                    isError = true;
                    listener.onError();
                }
            }
        }
    };

    private NativeAdvertManager() {
    }

    public static NativeAdvertManager getInstance() {
        if (null == instance) {
            synchronized (NativeAdvertManager.class) {
                if (null == instance) {
                    instance = new NativeAdvertManager();
                }
            }
        }
        return instance;
    }

    public void loadExpressAd(Activity activity, InteractionAdListener listener) {
        loadExpressAd(activity, AdvertConstants.NATIVE_TYPE_COMMON, listener);
    }

    public void loadExpressAd(Activity activity, String advertType, InteractionAdListener listener) {
        loadExpressAd(activity, advertType, 270, listener);
    }

    public void loadExpressAd(Activity activity, int width, InteractionAdListener listener) {
        loadExpressAd(activity, AdvertConstants.NATIVE_TYPE_COMMON, width, listener);
    }

    public void loadExpressAd(Activity activity, String advertType, int width, InteractionAdListener listener) {
        if(CommitUtils.isVivoClose() || CommitUtils.isHuaweiClose()){
            if (listener != null) {
                listener.onError();
            }
            return;
        }
        AdvertConfigList config = ShuaApplication.advertConfig;
        if(config == null){
            if (listener != null) {
                listener.onError();
            }
            return;
        }
        PatchConfigBean patchConfig = config.getPatch();
        if(patchConfig == null){
            if (listener != null) {
                listener.onError();
            }
            return;
        }
        AdvertConfigBean configBean = patchConfig.getConfigByType(advertType);
        if(configBean == null){
            if (listener != null) {
                listener.onError();
            }
            return;
        }
        isError = false;
        Message msg = new Message();
        msg.what = 1;
        msg.obj = listener;
        handler.sendMessageDelayed(msg, DELAY_TIME);
        String codeId = configBean.getOrigin_id();
        if(configBean.isTencent()){
            NativeAdvertTencentManager.getInstance().loadExpressAd(activity, advertType, codeId, width, new InteractionAdListener() {
                @Override
                public void onRenderSuccess(View view) {
                    handler.removeMessages(1);
                    if (listener != null && !isError) {
                        listener.onRenderSuccess(view);
                    }
                }

                @Override
                public void onError() {
                    //优量汇广告加载失败，补位穿山甲广告
                    isByte = true;
                    NativeAdvertByteManager.getInstance().loadExpressAd(activity, advertType, width, new InteractionAdListener() {
                        @Override
                        public void onRenderSuccess(View view) {
                            handler.removeMessages(1);
                            if (listener != null) {
                                listener.onRenderSuccess(view);
                            }
                        }

                        @Override
                        public void onError() {
                            handler.removeMessages(1);
                            if (isError) return;
                            if (listener != null) {
                                listener.onError();
                            }
                        }
                    });
                }
            });
        }else{
            NativeAdvertByteManager.getInstance().loadExpressAd(activity, advertType, codeId, width, new InteractionAdListener() {
                @Override
                public void onRenderSuccess(View view) {
                    handler.removeMessages(1);
                    if (listener != null && !isError) {
                        listener.onRenderSuccess(view);
                    }
                }

                @Override
                public void onError() {
                    //穿山甲广告加载失败，补位优量汇广告（改为补穿山甲）
                    isByte = false;
                    NativeAdvertByteManager.getInstance().loadExpressAd(activity, advertType, width, new InteractionAdListener() {
                        @Override
                        public void onRenderSuccess(View view) {
                            handler.removeMessages(1);
                            if (listener != null) {
                                listener.onRenderSuccess(view);
                            }
                        }

                        @Override
                        public void onError() {
                            handler.removeMessages(1);
                            if (isError) return;
                            if (listener != null) {
                                listener.onError();
                            }
                        }
                    });
                }
            });
        }
    }

}
